package com.wisdytech.linkdcs.linkdcsservicepad.pad.service.impl;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.wisdytech.common.constants.CommonConstants;
import com.wisdytech.linkdcs.linkdcsservicepad.common.exception.ServiceException;
import com.wisdytech.linkdcs.linkdcsservicepad.common.handler.BaseService;
import com.wisdytech.linkdcs.linkdcsservicepad.pad.service.ISystemService;
import com.wisdytech.linkdcs.system.dao.ISysUserDao;
import com.wisdytech.linkdcs.system.model.SysUser;
import groovy.util.logging.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service("systemUserService")
@Transactional
public class SystemServiceImpl extends BaseService<ISysUserDao, SysUser> implements ISystemService {

	@Resource
	ISysUserDao systemUserDao;

	private Logger logger = LoggerFactory.getLogger(getClass());

	@Override
	public boolean login(String username, String password) {
		EntityWrapper<SysUser> ew = new EntityWrapper<>();
		List<SysUser> users =
				 systemUserDao.selectList(ew.eq("username", username).eq("del_flag",  CommonConstants.DEL_FLAG));
		if(null == users || users.size() < 1) {
			logger.error("登陆账号密码错误");
			throw new ServiceException("账户不存在");
		}
		
		if(!users.get(0).getPassword().equals(password)) {
			throw new ServiceException("密码错误");
		}
		
		
		return true;
	}

	public List<String> getUserResource(String username) {
		//TODO 暂时未定，假数据
		List<String> resources =new ArrayList<>();
		resources.add("equipCheck.pda");
		resources.add("malfunctionRegister.pda");
		resources.add("malfunctionProcess.pda");
		resources.add("feeding.pda");
		resources.add("generateWIP.pda");
		resources.add("dust.pda");
		resources.add("messegeCenter.pda");
		resources.add("quxception.pda");
		return resources;
	}

	@Override
	public List<SysUser> getUsers() {
		EntityWrapper<SysUser> ew = new EntityWrapper<>();
		return systemUserDao.selectList(ew.eq("del_flag", CommonConstants.DEL_FLAG));
		
	}

	@Override
	public SysUser getUserInfo(String username) {
		SysUser user = new SysUser();
		user.setUsername(username);
		return systemUserDao.selectOne(user);
	}

}
